from dvadmin.system.models import Device
from dvadmin.utils.json_response import SuccessResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.viewset import CustomModelViewSet


class DeviceSerializer(CustomModelSerializer):

    class Meta:
        model = Device
        fields = '__all__'

class DeviceViewSet(CustomModelViewSet):

    """
    设备管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """

    queryset = Device.objects.all()
    serializer_class = DeviceSerializer

    def list(self, request, *args, **kwargs):
        # 如果懒加载，则只返回父级
        request.query_params._mutable = True
        params = request.query_params
        page = params.get('page', None)
        limit = params.get('limit', None)
        if page:
            del params['page']
        if limit:
            del params['limit']
        lst = Device.objects.filter(mer_id='55800736')
        serializer = DeviceSerializer(lst, many=True, request=request)
        data = serializer.data
        return SuccessResponse(data=data)